<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@include file="import.jsp" %>

<head>
    <LINK href="main.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sync file</title>
</head>
<body>

<jsp:include page="head.jsp"></jsp:include>

<div id="mainDiv">
<div id="localDiv">
	<div class="headMsg">
		Local Files
		<a href="./index.jsp">Home</a>
	</div>
	<div id="showFileDiv" class="fileWindow">
		<%
			SyncDirectory directory = (SyncDirectory)request.getAttribute("syncDirectory");
		%>
		<div id="baseFileDiv">
			<input type="checkbox" id="selectAll" name="selectAll"></input>
			<%
			if(ListLocalFilesServlet.baseDirs.contains(directory.getPath())){
			%>
			<%=directory.getPath()%>
			<%} else { %>
				<a href="listLocalFiles.do?subDir=<%=directory.getParentPath()%>"><%=directory.getPath()%></a>
			<%} %>
		</div>
		<div id="subFilesDiv">
		<%
			int i=0;
			for (SyncObject file : directory.getSubDirectories()) {
		%>
		 	<input type="checkbox" name="syncObject_<%=i++%>" class="class_syncObject" value="<%=file.getPath()%>"></input> 
		 	<% if(file.isFolder()){ %>
		 	<img src="./images/folder_open.png"></img><a href="listLocalFiles.do?subDir=<%=directory.getPath() + file.getPath()%>"><%=file.getPath()%></a>
		 	<%} else { %>
		 	<img src="./images/file.png"></img><%=file.getPath()%>
		 	<%} %>
		 	<br />
		<%
			}
		%>
		<form action="syncFiles.do" id="syncForm" name="syncForm" method="post">
			<input type="hidden" id="baseDirToSync" name="baseDirToSync" value="<%=directory.getPath()%>"/>
			<input type="hidden" id="filesToSync" name="filesToSync" />
			<input type="hidden" id="syncDestDir" name="syncDestDir" />
		</form>
		</div>
	</div>
</div>

<%@include file="server.jsp" %>
</div>

<script src="js/jquery.min.js"></script>
<script type="text/javascript" language="JavaScript">
function handle(){
	var directory = document.getElementById("fileSelector").value;
	document.getElementById("directory").value = directory;
	return true;
}

$(document).ready(function() {
	$("#selectAll").click(function() {
		var checkAll = $(this).attr("checked");
		if(checkAll){
			$.each($(".class_syncObject"), function(index, syncObj){
				syncObj.checked = true;
			});
		} else {
			$.each($(".class_syncObject"), function(index, syncObj){
				syncObj.checked = false;
			});
		}
	});
	
	$("#syncButton").click(function() {
		var filesToSync = '';
		$.each($(".class_syncObject"), function(index, syncObj){
			if($(syncObj).attr("checked") == 'checked'){
				filesToSync += $(syncObj).val() + ';';
			}
		});
		$("#filesToSync").val(filesToSync);
		$("#syncForm").submit();
	});
	
});
</script>

</body>

</html>